package com.mdnote.practice.stack;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Stack;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/11/22 21:32
 * @Email rhythm_2019@163.com
 * @Description 496. 下一个更大元素 I
 */
public class LeetCode496 {

    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Stack<Integer> stack = new Stack<>();
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = nums2.length - 1; i >= 0; i--) {
            int e = nums2[i];
            while (!stack.isEmpty() && e > stack.peek()) {
                stack.pop();
            }
            map.put(e, stack.isEmpty() ? -1 : stack.peek());
            stack.push(e);
        }

        int[] ans = new int[nums1.length];
        for (int i = 0; i < nums1.length; i++) {
            ans[i] = map.get(nums1[i]);
        }

        return ans;
    }

}
